#!/bin/sh
. /etc/utils.sh

if ! is-html-tv-ui; then
  echo "configured for sage"
  exit 0
fi

case "$1" in
  start)
    (
      wait-until-created /tmp/nexus_multiprocess
      rc_pipe_init
      if is-tv-box; then
        babysit 10 keep_oregano_nice 2>&1 | logos keep-oregano-nice 0 0 &
        setuid appclient:video /app/oregano/runbasil 2>&1 | logos basil 0 20000000 &
      fi
    ) &
    ;;
  stop)
    # End the Basil dart server (which will also end its babysitter).
    pkillwait -f '(dart.*)([b]asil(_nocast)?.*\.dart)'
    pkillwait -f -x '/bin/sh /bin/keep_oregano_nice'
    pkillwait -f -x 'wait-until-created /tmp/renice_oregano'
    rc_pipe_deinit
    ;;
  restart)
    $0 stop; $0 start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac
